package com.learn.executor;

import javax.sound.midi.Soundbank;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 异步任务task
 * @author chenxin
 * @time 2017-03-14-22:38
 */
public class ExecutorException {
    public static void main(String[] args) {
        ExecutorService executor = Executors.newCachedThreadPool();

        Future<Void> futureTask = null;
        try {

            futureTask = (Future<Void>) executor.submit(new Runnable() {
                @Override public void run() {
                    int x = 5 / 0;
                    System.out.println(x);
                }
            });
        } catch (Exception e){
            System.out.println("1");
        }

        try {
            futureTask.get();

        } catch (ArithmeticException e){
            System.out.println("2");
        } catch (Exception e){
            System.out.println("3");
            System.out.println("*******");
            System.out.println(e.getCause().getClass());
        }

        executor.shutdown();
        try {
            A a = new A();
            Method m = A.class.getMethod("b");
            m.invoke(a);
        } catch (RuntimeException e) {
            e.printStackTrace();
            System.out.println(1);
        } catch (InvocationTargetException e1) {
            e1.printStackTrace();
            e1.getTargetException();
            System.out.println(2);
        } catch (Exception ew) {
            ew.printStackTrace();
            System.out.println(3);

        }

//        System.out.println("*******");
//        System.out.println(e.getCause().getClass());
    }
    public static class A{
        public void b() {
            throw new RuntimeException();
        }
    }
}
